﻿using System;
using System.Web.Mvc;
using CarsRegistrationService.Managers.Interfaces;

namespace CarsRegistrationService.Controllers
{
    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")] 
    public class MobileController : Controller
    {
        private IMobileManager mobileManager;

        public MobileController(IMobileManager mobileManager)
        {
            this.mobileManager = mobileManager;
        }

        #region Inspector

        [HttpPost]
        public ActionResult Registration(string login, string passHash, string firstName,
                                         string lastName, string middleName,
                                         string passportSeries, string passportNumber)
        {
            var result = this.mobileManager.Registration(login, passHash, firstName,
                                            lastName, middleName,
                                            passportSeries, passportNumber);
            int code = 0;
            if (!string.IsNullOrWhiteSpace(result))
            {
                code = -1;
            }
            return Json(new { ResultCode = code, Result = result ?? "Error" },
                JsonRequestBehavior.DenyGet);
        }
        [HttpGet]
        public ActionResult Login(string login, string passHash)
        {
            if (this.mobileManager.IsUserDataCorrect(login, passHash))
            {
                return Json(new { ResultCode = 0, Result = "OK" },
                    JsonRequestBehavior.AllowGet);
            }
            return Json(new {ResultCode = -1, Result = "Неверный логин или пароль"},
                JsonRequestBehavior.AllowGet);
        }

        #endregion Inspector

        #region Request

        [HttpGet]
        public ActionResult CreateRequest(string login, string ownerName, string vehicleNumber)
        {
            this.mobileManager.DirectoryName = Server.MapPath("RequestData");
            Guid requestID = this.mobileManager.CreateRequest(login, ownerName, vehicleNumber);
            return Json(new { RequestID = requestID.ToString(), Result = "OK" },
                JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult DeleteRequest(string requestID)
        {
            Guid id = Guid.Parse(requestID);
            string result = this.mobileManager.DeleteRequest(id);
            return Json(new { result = result }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult SaveImage(string requestID, string imageBase64,
                                      bool? first, bool? last)
        {
            try
            {
                this.mobileManager.AddImageToRequest(requestID, imageBase64,
                    first ?? false, last ?? false);
                return Json(new { Result = "OK" }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception error)
            {
                return Json(new { Result = "ServerError", ResultCode = "-1" }, JsonRequestBehavior.AllowGet);
            }
        }

        #endregion Request
    }
}
